package com.cq.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cq.health.common.RedisConst;
import com.cq.health.dao.SetmealDao;
import com.cq.health.entity.PageResult;
import com.cq.health.entity.QueryPageBean;
import com.cq.health.pojo.Setmeal;
import com.cq.health.service.SetmealService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/5/31 20:35
 * @Description: 套餐业务实现类
 * @Version: 1.0
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Transactional
    @Override
    public void add(Setmeal setmeal, Integer[] checkGroupIds) {
        log.debug("SetmealServiceImpl setmeal:{},checkGroupIds:{}",setmeal,checkGroupIds);
        //新增套餐
        setmealDao.add(setmeal);
        //新增关系
        for (Integer checkGroupId : checkGroupIds) {
            Map oneMap = new HashMap();
            oneMap.put("setmeal_id",setmeal.getId());
            oneMap.put("checkgroup_id",checkGroupId);
            setmealDao.addSetmealAndCheckGroup(oneMap);
        }
        jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //调用dao获取page
        Page<Setmeal> pages = setmealDao.pageQuery(queryPageBean.getQueryString());
        //构建PageResult并返回
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findById(Integer id) {
        log.debug("findById id:"+id);
        return setmealDao.findById(id);
    }
}
